package cn.duli.myvideo.controller;


import cn.duli.myvideo.pojo.CollectMovie;
import cn.duli.myvideo.service.CollectMovieService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/collectMovie")
public class CollectMovieController {

    @Resource
    private CollectMovieService collectMovieService;

    /**
     * 分页查询所有收藏电影列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/findAll")
    public PageInfo<CollectMovie> findAll(
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "2") int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<CollectMovie> pageInfo = new PageInfo<>(collectMovieService.findAll());
        return pageInfo;
    }


    /**
     * 根据电影id查找此电影收藏的总数
     * @param mId
     * @return
     */
    @GetMapping("/findCountByMovieId/{mId}")
    public Integer findCountByMovieId(@PathVariable("mId") Integer mId){
        return collectMovieService.findCountByMovieId(mId);
    }

    /**
     * 根据用户id查看该用户收藏的电影
     * @param uId
     * @return
     */
    @GetMapping("/findByUserId/{uId}")
    public List<CollectMovie> findByUserId(@PathVariable("uId") Integer uId){
        return collectMovieService.findByUserId(uId);
    }

    /**
     * 根据电影id查看该电影的收藏列表
     * @param mId
     * @return
     */
    @GetMapping("/findByMovieId/{mId}")
    public List<CollectMovie> findByMovieId(@PathVariable("mId") Integer mId){
        return collectMovieService.findByMovieId(mId);
    }

    /**
     * 新增收藏电影
     * @param collectMovie
     */
    @PostMapping("/save")
    public void save(@RequestBody CollectMovie collectMovie){
        collectMovieService.save(collectMovie);
    }

    /**
     * 删除收藏电影
     * @param cmId
     */
    @RequestMapping("/deleteById/{cmId}")
    public void deleteById(@PathVariable("cmId") Integer cmId){
        collectMovieService.deleteById(cmId);
    }
}
